/**
 *
 */
package steven.sth;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author steven.lam.t.f
 *
 */
public class S20141020MonospaceFont{
	public static final void main(final String[] args){
		final GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
		final String[] fontFamilyNames = graphicsEnvironment.getAvailableFontFamilyNames();
		final BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
		final Graphics graphics = bufferedImage.createGraphics();
		final int fontSize = 20;
		final int[] fontStyles = new int[]{Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD | Font.ITALIC};
		final Map<String, Integer> widthMap = new HashMap<>();
		widthMap.put("1234567890", fontSize * 10 / 2);
		widthMap.put("ABCDEFGHIJ", fontSize * 10 / 2);
		widthMap.put("abcdefghij", fontSize * 10 / 2);
		widthMap.put("一二三四五六七八九十", fontSize * 10);
		widthMap.put("123ABCabc一二三", fontSize * 15 / 2);
		for(final String fontFamilyName : fontFamilyNames){
			boolean isMonospaced = true;
			for(final int fontStyle : fontStyles){
				final Font font = new Font(fontFamilyName, fontStyle, fontSize);
				final FontMetrics fontMetrics = graphics.getFontMetrics(font);
				int firstCharacterWidth = 0;
				boolean hasFirstCharacterWidth = false;
				for(int codePoint = 0; codePoint < 128; codePoint++){
					if(Character.isValidCodePoint(codePoint) && (Character.isLetter(codePoint) || Character.isDigit(codePoint))){
						final char character = (char)codePoint;
						final int characterWidth = fontMetrics.charWidth(character);
						if(hasFirstCharacterWidth){
							if(characterWidth != firstCharacterWidth){
								isMonospaced = false;
								break;
							}
						}else{
							firstCharacterWidth = characterWidth;
							hasFirstCharacterWidth = true;
						}
					}
				}
				boolean test = true;
				for(final Entry<String, Integer> entry : widthMap.entrySet()){
					if(fontMetrics.stringWidth(entry.getKey()) == entry.getValue() == false){
						test = false;
						break;
					}
				}
				if(test){
					System.out.println(font + " " + isMonospaced);
				}
			}
		}
		graphics.dispose();
	}
	public static final void getMonospace(final Graphics g){
		final GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
		final String[] fontFamilyNames = graphicsEnvironment.getAvailableFontFamilyNames();
		final int fontSize = 20;
		final Map<String, Integer> widthMap = new HashMap<>();
		widthMap.put("1234567890", fontSize * 10 / 2);
		widthMap.put("ABCDEFGHIJ", fontSize * 10 / 2);
		widthMap.put("abcdefghij", fontSize * 10 / 2);
		widthMap.put("一二三四五六七八九十", fontSize * 10);
		widthMap.put("123ABCabc一二三", fontSize * 15 / 2);
		for(final String fontFamilyName : fontFamilyNames){
			boolean isMonospaced = true;
			final int fontStyle = Font.PLAIN;
			final Font font = new Font(fontFamilyName, fontStyle, fontSize);
			final FontMetrics fontMetrics = g.getFontMetrics(font);
			int firstCharacterWidth = 0;
			boolean hasFirstCharacterWidth = false;
			for(int codePoint = 0; codePoint < 128; codePoint++){
				if(Character.isValidCodePoint(codePoint) && (Character.isLetter(codePoint) || Character.isDigit(codePoint))){
					final char character = (char)codePoint;
					final int characterWidth = fontMetrics.charWidth(character);
					if(hasFirstCharacterWidth){
						if(characterWidth != firstCharacterWidth){
							isMonospaced = false;
							break;
						}
					}else{
						firstCharacterWidth = characterWidth;
						hasFirstCharacterWidth = true;
					}
				}
			}
			boolean test = true;
			for(final Entry<String, Integer> entry : widthMap.entrySet()){
				if((fontMetrics.stringWidth(entry.getKey()) == entry.getValue()) == false){
					test = false;
					break;
				}
			}
			if(test){
				System.out.println(fontFamilyName + " " + isMonospaced);
				for(final Entry<String, Integer> entry : widthMap.entrySet()){
					System.out.println(entry.getKey() + " " + fontMetrics.stringWidth(entry.getKey()));
				}
			}
		}
	}
	public static final void test(final Graphics g, final Font font){
		System.out.println("test " + font);
		final int fontSize = 20;
		final Map<String, Integer> widthMap = new HashMap<>();
		widthMap.put("1234567890", fontSize * 10 / 2);
		widthMap.put("ABCDEFGHIJ", fontSize * 10 / 2);
		widthMap.put("abcdefghij", fontSize * 10 / 2);
		widthMap.put("一二三四五六七八九十", fontSize * 10);
		widthMap.put("123ABCabc一二三", fontSize * 15 / 2);
		boolean isMonospaced = true;
		final FontMetrics fontMetrics = g.getFontMetrics(font);
		int firstCharacterWidth = 0;
		boolean hasFirstCharacterWidth = false;
		for(int codePoint = 0; codePoint < 128; codePoint++){
			if(Character.isValidCodePoint(codePoint) && (Character.isLetter(codePoint) || Character.isDigit(codePoint))){
				final char character = (char)codePoint;
				final int characterWidth = fontMetrics.charWidth(character);
				if(hasFirstCharacterWidth){
					if(characterWidth != firstCharacterWidth){
						isMonospaced = false;
						break;
					}
				}else{
					firstCharacterWidth = characterWidth;
					hasFirstCharacterWidth = true;
				}
			}
		}
		boolean test = true;
		for(final Entry<String, Integer> entry : widthMap.entrySet()){
			if((fontMetrics.stringWidth(entry.getKey()) == entry.getValue()) == false){
				test = false;
				break;
			}
		}
		if(test){
			System.out.println(font + " " + isMonospaced);
			for(final Entry<String, Integer> entry : widthMap.entrySet()){
				System.out.println(entry.getKey() + " " + fontMetrics.stringWidth(entry.getKey()));
			}
		}
	}
}
